//package com.zhangdi.demo.gateway.listener;
//
//import com.alibaba.cloud.nacos.NacosConfigManager;
//import com.alibaba.cloud.nacos.NacosConfigProperties;
//import com.alibaba.cloud.nacos.NacosServiceManager;
//import com.alibaba.nacos.api.config.listener.Listener;
//import com.alibaba.nacos.api.exception.NacosException;
//import com.google.gson.Gson;
//import com.google.gson.GsonBuilder;
//import com.google.gson.reflect.TypeToken;
//import org.springframework.beans.BeansException;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.cloud.gateway.event.RefreshRoutesEvent;
//import org.springframework.cloud.gateway.route.RouteDefinition;
//import org.springframework.cloud.gateway.route.RouteDefinitionRepository;
//import org.springframework.context.ApplicationContext;
//import org.springframework.context.ApplicationContextAware;
//import org.springframework.stereotype.Component;
//import reactor.core.publisher.Flux;
//import reactor.core.publisher.Mono;
//
//import java.util.ArrayList;
//import java.util.List;
//import java.util.concurrent.Executor;
//
//@Component
//public class NacosRouteDefinitionListener implements RouteDefinitionRepository, ApplicationContextAware {
//    @Autowired
//    NacosConfigProperties nacosConfigProperties;
//    @Autowired
//    NacosConfigManager nacosConfigManager;
//
//    private final String dataId = "gatewayId";
//    private final String groupId = "gatewayGroup";
//
//    @Override
//    public Flux<RouteDefinition> getRouteDefinitions() {
//        List<RouteDefinition> list = new ArrayList<>();
//        try {
//            String content = nacosConfigManager.getConfigService().getConfig(dataId, groupId, 5000);
//            Gson gson = new Gson();
//            List<RouteDefinition> tmpList = gson.fromJson(content, new TypeToken<List<RouteDefinition>>() {
//            }.getType());
//            if (null != tmpList) {
//                list = tmpList;
//            }
//
//        } catch (NacosException e) {
//            e.printStackTrace();
//        }
//        return Flux.fromIterable(list);
//    }
//
//    @Override
//    public Mono<Void> save(Mono<RouteDefinition> route) {
//        return null;
//    }
//
//    @Override
//    public Mono<Void> delete(Mono<String> routeId) {
//        return null;
//    }
//
//    @Override
//    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
//        try {
//            nacosConfigManager.getConfigService().addListener(dataId, groupId, new Listener() {
//                @Override
//                public Executor getExecutor() {
//                    return null;
//                }
//
//                @Override
//                public void receiveConfigInfo(String s) {
//                    applicationContext.publishEvent(new RefreshRoutesEvent(this));
//                }
//            });
//        } catch (NacosException e) {
//            e.printStackTrace();
//        }
//    }
//}
